package utils

import (
	"strings"
	"time"
)

func GetTmShortStr(tm time.Time, format string) string {
	patterns := []string{
		"y", "06",
		"m", "1",
		"d", "2",

		"Y", "06",
		"M", "1",
		"D", "2",

		"h", "3", //12小时制
		"H", "15", //24小时制

		"i", "4",
		"s", "5",

		"t", "pm",
		"T", "PM",
	}

	return convStr(tm, format, patterns)
}

func convStr(tm time.Time, format string, patterns []string) string {
	replacer := strings.NewReplacer(patterns...)
	strfmt := replacer.Replace(format)
	return tm.Format(strfmt)
}

//生成当前时间,格式是 "Y-m-d H:i:s"
func NowYhmHis() string {
	s := GetTmShortStr(time.Now(), "Y-m-d H:i:s")
	return s
}
